const {
  STORAGE_CONSTANT,
  USER_TYPE
} = require('../constant/index')

function toString(data) {
  return JSON.stringify(data)
}

function toParse(data) {
  return JSON.parse(data)
}
export function setToken(data) {
  wx.setStorageSync(STORAGE_CONSTANT.token, data)
}

export function getToken() {
  return wx.getStorageSync(STORAGE_CONSTANT.token)
}

export function clearAll() {
  Object.keys(STORAGE_CONSTANT).forEach(key => {
    wx.removeStorageSync(STORAGE_CONSTANT[key])
  })
}

export function setUserType(data) {
  wx.setStorageSync(STORAGE_CONSTANT.userType, data)
}

export function getUserType() {
  return wx.getStorageSync(STORAGE_CONSTANT.userType)
}

export function setUserBaseInformation(data) {
  wx.setStorageSync(STORAGE_CONSTANT.userBaseInfomation, toString(data))
}

export function getUserBaseInformation() {
  let data = null
  try {
    data = toParse(wx.getStorageSync(STORAGE_CONSTANT.userBaseInfomation))
  } catch {}
  return data
}

export function getUserKind() {
  const type = getUserType()
  if ([USER_TYPE.CUSTOMER_MANAGER, USER_TYPE.CUSTOMER_USER].includes(type)) {
    return 'client'
  }
  return 'repair'
}

export function setApprovalCustomerWorkOrder(data) {
  wx.setStorageSync(STORAGE_CONSTANT.approveCustomerWorkOrder, data)
}

export function getApproveCustomerWordOrder() {
  return wx.getStorageSync(STORAGE_CONSTANT.approveCustomerWorkOrder)
}